jQuery(document).ready(function($) {

/*---------------------------------------------------------------------------*
 * Scroll to top
/*---------------------------------------------------------------------------*/
	$('#footer a.top,.sc-divider a').click(function() {
		$('html, body').animate({scrollTop:0},'slow');
		return false;
	});
	
/*---------------------------------------------------------------------------*
 * Front Slider
/*---------------------------------------------------------------------------*/
	$(window).load(function() {
		if($("#front-slider-container").length) {
			$(window).resize(function() {
				$("#front-slider-container").carouFredSel({
					width	: $("#front-slider-container").parents("#front-slider").innerWidth(),
					auto	: false,
					items	: 1,
					scroll	: 1,
					prev	: { button : "#slide-prev", key : "left" },
					next	: { button	: "#slide-next", key : "right" },
					align	: false,
				});	
	        }).resize();
		}
	});
	
/*---------------------------------------------------------------------------*
 * Menu fade
/*---------------------------------------------------------------------------*/
	$('.nav ul.sub-menu').hide();
	$('.nav li').hover( 
		function() {
			$(this).children('ul.sub-menu').slideDown('fast');
		}, 
		function() {
			$(this).children('ul.sub-menu').hide();
		}
	);

/*---------------------------------------------------------------------------*
 * Toggle archive
/*---------------------------------------------------------------------------*/	
	$("#toggle-archive").click(function(event) {
		event.preventDefault();
		$(this).toggleClass('active');
		// Do we need to get data?
		if(!$('#headexpand-inner').length) {
			// Get archive template
			$.ajax({
				url: bandit.ajaxurl,
				type: "POST",
				dataType: 'html',
				data: {
						action : 'creatic_ajaxarchive',
						nonce : bandit.ajaxnonce,
				},
				success:function(data) {
					if(data != 'error') {
						$('#headexpand').append(data).slideToggle();
					}
				}
			});
		} else {
			// Toggle archive div
			$('#headexpand').delay(500).slideToggle();
		}
		
	});
	
/*---------------------------------------------------------------------------*
 * Masonry
/*---------------------------------------------------------------------------*/	
	$('#masonry').masonry({
		itemSelector: '.block',
		isAnimated: true
	});

	
/*---------------------------------------------------------------------------*
 * Show/Hide Category List
/*---------------------------------------------------------------------------*/
	if($('.category-expand').length) {
		$('.format-icon').hover(
			function() {
				$(this).children('.category-expand').animate({width:'toggle'},'fast');
			},
			function() {
				$(this).children('.category-expand').hide();
			}
		);
	}

/*---------------------------------------------------------------------------*
 * Add alt-row styling to tables
/*---------------------------------------------------------------------------*/
	if($('.entry table').length) {
		$('.entry table tr:odd').addClass('alt-table-row');
	}

/*---------------------------------------------------------------------------*
 * jQuery UI Tabs
/*---------------------------------------------------------------------------*/
	// widget tabs
	if($('.widget_bandit_tabs').length) {
		$('.bandit_tabs').tabs({
			fx: {opacity:'show'}
		});
	}

	// content tabs
	if($('.sc-tabs').length) {
		$('.sc-tabs').tabs({
			fx: {opacity:'show'}
		});
	}

/*---------------------------------------------------------------------------*
 * Toggle Box
/*---------------------------------------------------------------------------*/
	if($('.toggle-box').length) {
		$('h4.toggle-title').click(function() {
			$(this).toggleClass('toggle-title-active')
			$(this).parent().find('.toggle-inner').toggle();
			return false;
		});
	}

/*---------------------------------------------------------------------------*
 * Like This
/*---------------------------------------------------------------------------*/
	if($('.like-count').length) {
		$('.like-count a').live('click',function() {
			var id = $(this).attr('id');
			id = id.split('like-');
			$.ajax({
				url: bandit.ajaxurl,
				type: "POST",
				dataType: 'json',
				data: { action : 'bandit_liked_ajax', id : id[1] },
				success:function(data) {
					if(true==data.success) {
						$('#like-'+data.postID).html('<i class="icon"></i> '+data.count);
						$('#like-'+data.postID).addClass('active');
					}
				}
			});
			return false;
		});
	}

/*---------------------------------------------------------------------------*
 * prettyPhoto
/*---------------------------------------------------------------------------*/
	function init_pp() {
		if($('.prettyPhoto').length) {
			$("a[rel^='prettyPhoto']").prettyPhoto({
				default_width: 620,
				overlay_gallery: false,
				social_tools: ''
			});
		}
	}
	init_pp();
	

/*---------------------------------------------------------------------------*
 * Blog AJAX Nav
/*---------------------------------------------------------------------------*/
	if($('#blog-load-more-link').length) {
		// Create namespace
		var args = {};
		args.offset = 0;
		// Variables
		var loadMoreLink = $('#blog-load-more-link');
		var numposts = loadMoreLink.attr('data-numposts');
		var postsperpage = parseInt(loadMoreLink.attr('data-postsperpage'));
		args.catid = loadMoreLink.attr('data-catid');
		args.tagid = loadMoreLink.attr('data-tagid');
		args.authorid = loadMoreLink.attr('data-authorid');
		args.search = loadMoreLink.attr('data-search');

		loadMoreLink.click(function() {
			// Set offset
			args.offset = args.offset + postsperpage;
			// Can we load more posts?
			if(numposts > 0) {
				// Change button text
				loadMoreLink.text('Loading ...');
				// Get posts
				$.ajax({
					url: bandit.ajaxurl,
					type: "POST",
					dataType: 'html',
					data: {
							action : 'creatic_ajaxnav',
							nonce : bandit.ajaxnonce,
							args : args
					},
					success:function(data) {
						if(data != 'error') {
							$('#masonry').append(data);
							$('#masonry').masonry('reload');
							init_pp();
						}
						
					}
				});
				// Change text back
				loadMoreLink.text('Load More');
			}

			// Reduce number of available posts
			numposts = numposts - postsperpage;

			// Hide link if no more posts
			if(numposts <= postsperpage) {
				loadMoreLink.hide();
			}
			
			return false;
		});
	}

/*---------------------------------------------------------------------------*
 * Portfolio AJAX Nav
/*---------------------------------------------------------------------------*/
	if($('#entry-load-more-link').length) {
		// variables
		var loadMoreLink = $('#entry-load-more-link');
		var maxPages = parseInt(loadMoreLink.attr('data-max'));
		var currentPage = 1;
		var nextLink = loadMoreLink.attr('data-src');
		// load more link click function
		loadMoreLink.click(function() {
			if(currentPage < maxPages) {
				currentPage++;
				$(this).text('Loading ...');
				$.get(nextLink+currentPage+' .item', function(data) {
					$(data).find('article').appendTo('#masonry');
					$('#masonry').masonry('reload');
					// Check if more pages
					if(currentPage < maxPages) {
						loadMoreLink.text('Load More');
					} else {
						loadMoreLink.hide();
					}
					// Reload prettyPhoto if needed
					if($('.prettyPhoto').length) {
						$("a[rel^='prettyPhoto']").prettyPhoto({
							default_width: 620,
							overlay_gallery: false,
							social_tools: ''
						});
					}
				});
			}
			return false;
		});
	}

/*---------------------------------------------------------------------------*
 * Entry Nav Width
/*---------------------------------------------------------------------------*/
	if($('.entry-nav').length) {
		// Create namespace
		var lm = {};

		// Resize function
		function loadMoreWidth() {
			lm.articleWidth = $('#masonry article').width() + 20;
			lm.masonryWidth = $('#masonry').width();
			lm.articleNum = Math.floor(lm.masonryWidth/lm.articleWidth);
			lm.navWidth = (lm.articleWidth * lm.articleNum) + 40;
			$('.entry-nav').stop().animate({width: lm.navWidth+'px'}, 400);
		}

		loadMoreWidth();

		$(window).resize(function() {
			loadMoreWidth();
		});
	}

});